package com.naiterui.ehp.bs.doctor.utils;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.doctor.vo.LimitConfigVO;
import com.naiterui.ehp.bs.doctor.vo.LimitValueVO;

/**
 * 客户端极限值配置Config
 *
 * @since 2.5.0
 */
public class LimitValueConfig {

    // 勿扰快捷回复(APP)
    public static final String LIMIT_DISTURBREPLY = "limit.disturbReply";
    // 个人简介
    public static final String LIMIT_INTRODUCTION = "limit.introduction";
    // 专业与专长
    public static final String LIMIT_EXPERTISE = "limit.expertise";
    // 自定义医院
    public static final String LIMIT_CUSTOMHOSPITAL = "limit.customHospital";
    // 自定义科室
    public static final String LIMIT_CUSTOMDEPARTMENT = "limit.customDepartment";
    // 银行卡卡号
    public static final String LIMIT_BANKCARDNUM = "limit.bankCardNum";
    // 银行卡卡号
    public static final String LIMIT_BANKCARDBRANCH = "limit.bankCardBranch";
    // 患者分组名称
    public static final String LIMIT_PATIENTGROUPNAME = "limit.patientGroupName";
    // 患者备注名称
    public static final String LIMIT_PATIENTREMARKNAME = "limit.patientRemarkName";
    // 咨询快捷回复
    public static final String LIMIT_QUICKREPLY = "limit.quickReply";
    // 患者病情备注-纯文本
    public static final String LIMIT_PATIENTRECORDTEXT = "limit.patientRecordText";
    // 患者病情备注-图文
    public static final String LIMIT_PATIENTRECORDPICTEXT = "limit.patientRecordPicText";
    // 推荐用药-药品数量
    public static final String LIMIT_RECOMNUM = "limit.recomNum";
    // 推荐用药-每日用量
    public static final String LIMIT_RECOMDAILYUSAGE = "limit.recomDailyUsage";
    // 推荐用药-每次用量
    public static final String LIMIT_RECOMUSAGE = "limit.recomUsage";
    // 推荐用药-自定义单位-自定义服用方式-自定义服用时间
    public static final String LIMIT_RECOMOTHERUNIT = "limit.recomOtherUnit";
    // 推荐用药-备注长度
    public static final String LIMIT_RECOMREMARK = "limit.recomRemark";
    // 推荐用药-建议有效期
    public static final String LIMIT_RECOMEXPIRATION = "limit.recomExpiration";
    // 推荐用药-自定义多少日
    public static final String LIMIT_RECOMDAY = "limit.recomDay";
    // 推荐用药-自定义周期多少周
    public static final String LIMIT_RECOMCYCLEDAY = "limit.recomCycleDay";
    // 用户信息-密码
    public static final String LIMIT_PASSWORD = "limit.password";
    // 用户信息-真实姓名
    public static final String LIMIT_REALNAME = "limit.realName";
    // 处方单名称
    public static final String LIMIT_MEDICATIONGROUPNAME = "limit.medicationGroupName";
    // 药品搜索-关键字
    public static final String LIMIT_MEDICINESEARCH = "limit.medicineSearch";
    // 患者搜索-关键字
    public static final String LIMIT_PATIENTSEARCH = "limit.patientSearch";
    // 联系人搜索-关键字
    public static final String LIMIT_PHONECONTACTSEARCH = "limit.phoneContactSearch";
    // 群发消息-内容长度
    public static final String LIMIT_BATCHMESSAGECONTENT = "limit.batchMessageContent";
    // 图文咨询收费金额(分)
    public static final String LIMIT_MESSAGE_CHARGE_AMOUNT = "limit.message.charge.amount";
    // 个性化咨询收费金额(分)
    public static final String LIMIT_CUSTOM_CHARGE_AMOUNT = "limit.custom.charge.amount";
    // 个性化咨询收费名称
    public static final String LIMIT_CUSTOM_CHARGE_NAME = "limit.custom.charge.name";
    // 个性化咨询收费简介
    public static final String LIMIT_CUSTOM_CHARGE_SYNOPSIS = "limit.custom.charge.synopsis";
    // 医生备注患者个人信息，地址长度
    public static final String LIMIT_PATIENT_ADDRESS = "limit.patient.address";
    // 医生备注患者病情信息，相关病史描述
    public static final String LIMIT_PATIENT_DISEASE = "limit.patient.disease";
    // 患者病例图片数量
    public static final String LIMIT_PATIENTRECORD_PIC_NUM = "limit.patientRecord.pic.num";
    // 主诉
    public static final String LIMIT_DISEASE_MAINCOMPLAINT = "limit.disease.mainComplaint";
    // 现病史
    public static final String LIMIT_DISEASE_PRESENTDISEASE = "limit.disease.presentDisease";
    // 既往史
    public static final String LIMIT_DISEASE_PAST = "limit.disease.past";
    // 体温
    public static final String LIMIT_DISEASE_TEMPERATURE = "limit.disease.temperature";
    // 体重
    public static final String LIMIT_DISEASE_WEIGHT = "limit.disease.weight";
    // 心率
    public static final String LIMIT_DISEASE_HEARTRETE = "limit.disease.heartRete";
    // 收缩压
    public static final String LIMIT_DISEASE_SYSTOLIC = "limit.disease.systolic";
    // 舒张压
    public static final String LIMIT_DISEASE_DIASTOLE = "limit.disease.diastole";
    // 更多检查结果
    public static final String LIMIT_DISEASE_MOREEXAMINE = "limit.disease.moreExamin";
    // alt
    public static final String LIMIT_DISEASE_ALT = "limt.disease.alt";
    // ast
    public static final String LIMIT_DISEASE_AST = "limt.disease.ast";
    // hbv_dna
    public static final String LIMIT_DISEASE_HBV_DNA = "limt.disease.hbv.dna";
    // 医嘱小结
    public static final String LIMIT_DISEASE_DOCTORORDER = "limit.disease.doctorOrder";

    /**
     * 获取所有配置
     * LimitValueVO.getTotalConfig()
     *
     * @Date 2016年7月1日
     * @since 2.5.0
     */
    public static LimitValueVO getLimitConfig() {

        LimitValueVO totalConfigVO = new LimitValueVO();

        totalConfigVO.setDisturbReply(parseLimitConfig(ConfigUtil.getString(LIMIT_DISTURBREPLY)));
        totalConfigVO.setIntroduction(parseLimitConfig(ConfigUtil.getString(LIMIT_INTRODUCTION)));
        totalConfigVO.setExpertise(parseLimitConfig(ConfigUtil.getString(LIMIT_EXPERTISE)));
        totalConfigVO.setCustomHospital(parseLimitConfig(ConfigUtil.getString(LIMIT_CUSTOMHOSPITAL)));
        totalConfigVO.setCustomDepartment(parseLimitConfig(ConfigUtil.getString(LIMIT_CUSTOMDEPARTMENT)));
        totalConfigVO.setBankCardNum(parseLimitConfig(ConfigUtil.getString(LIMIT_BANKCARDNUM)));
        totalConfigVO.setBankCardBranch(parseLimitConfig(ConfigUtil.getString(LIMIT_BANKCARDBRANCH)));
        totalConfigVO.setPatientGroupName(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENTGROUPNAME)));
        totalConfigVO.setPatientRemarkName(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENTREMARKNAME)));
        totalConfigVO.setQuickReply(parseLimitConfig(ConfigUtil.getString(LIMIT_QUICKREPLY)));
        totalConfigVO.setPatientRecordText(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENTRECORDTEXT)));
        totalConfigVO.setPatientRecordPicText(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENTRECORDPICTEXT)));
        totalConfigVO.setRecomNum(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMNUM)));
        totalConfigVO.setRecomDailyUsage(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMDAILYUSAGE)));
        totalConfigVO.setRecomUsage(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMUSAGE)));
        totalConfigVO.setRecomOtherUnit(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMOTHERUNIT)));
        totalConfigVO.setRecomRemark(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMREMARK)));
        totalConfigVO.setRecomExpiration(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMEXPIRATION)));
        totalConfigVO.setPassword(parseLimitConfig(ConfigUtil.getString(LIMIT_PASSWORD)));
        totalConfigVO.setRealName(parseLimitConfig(ConfigUtil.getString(LIMIT_REALNAME)));
        totalConfigVO.setMedicationGroupName(parseLimitConfig(ConfigUtil.getString(LIMIT_MEDICATIONGROUPNAME)));
        totalConfigVO.setMedicineSearch(parseLimitConfig(ConfigUtil.getString(LIMIT_MEDICINESEARCH)));
        totalConfigVO.setPatientSearch(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENTSEARCH)));
        totalConfigVO.setPhoneContactSearch(parseLimitConfig(ConfigUtil.getString(LIMIT_PHONECONTACTSEARCH)));
        totalConfigVO.setBatchMessageContent(parseLimitConfig(ConfigUtil.getString(LIMIT_BATCHMESSAGECONTENT)));
        totalConfigVO.setMessageChargeAmount(parseLimitConfig(ConfigUtil.getString(LIMIT_MESSAGE_CHARGE_AMOUNT)));
        totalConfigVO.setCustomChargeAmount(parseLimitConfig(ConfigUtil.getString(LIMIT_CUSTOM_CHARGE_AMOUNT)));
        totalConfigVO.setCustomChargeName(parseLimitConfig(ConfigUtil.getString(LIMIT_CUSTOM_CHARGE_NAME)));
        totalConfigVO.setCustomChargeSynopsis(parseLimitConfig(ConfigUtil.getString(LIMIT_CUSTOM_CHARGE_SYNOPSIS)));
        totalConfigVO.setPatientRemarkAddress(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENT_ADDRESS)));
        totalConfigVO.setPatientRemarkDisease(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENT_DISEASE)));
        totalConfigVO.setPatientRecordPicNum(parseLimitConfig(ConfigUtil.getString(LIMIT_PATIENTRECORD_PIC_NUM)));
        totalConfigVO.setRecomDay(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMDAY)));
        totalConfigVO.setRecomCycleDay(parseLimitConfig(ConfigUtil.getString(LIMIT_RECOMCYCLEDAY)));

        totalConfigVO.setCaseMainComplaint(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_MAINCOMPLAINT)));
        totalConfigVO.setCasePresentDisease(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_PRESENTDISEASE)));
        totalConfigVO.setCasePast(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_PAST)));
        totalConfigVO.setCaseTemperature(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_TEMPERATURE)));
        totalConfigVO.setCaseWeight(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_WEIGHT)));
        totalConfigVO.setCaseHeartRete(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_HEARTRETE)));
        totalConfigVO.setCaseSystolic(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_SYSTOLIC)));
        totalConfigVO.setCaseDiastole(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_DIASTOLE)));
        totalConfigVO.setCaseMoreExamin(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_MOREEXAMINE)));
        totalConfigVO.setCaseAlt(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_ALT)));
        totalConfigVO.setCaseAst(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_AST)));
        totalConfigVO.setCaseHbvDna(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_HBV_DNA)));
        totalConfigVO.setCaseDoctorOrder(parseLimitConfig(ConfigUtil.getString(LIMIT_DISEASE_DOCTORORDER)));
        return totalConfigVO;
    }

    /**
     * 长度校验
     * LimitValueConfig.validateLimit()
     *
     * @param configName LimitValueConfig.LIMIT_*
     * @param targetName
     *
     * @return
     *
     * @Date 2016年7月1日
     * @since 2.5.0
     */
    public static boolean validateLimit(String configName, String targetName) {

        LimitConfigVO configVO = parseLimitConfig(ConfigUtil.getString(configName));
        int max = null == configVO.getMax() ? 0 : configVO.getMax();
        int min = null == configVO.getMin() ? 0 : configVO.getMin();

        if (StringUtils.isBlank(targetName)) {
            return min == 0;
        }
        String regex = "^[\\s\\S]{" + min + "," + max + "}";

        return targetName.matches(regex);
    }

    /**
     * 加载某项配置值
     * LimitValueConfig.parseLimitConfig()
     *
     * @param string
     *
     * @return EXAMPLE1：
     * 19,1,10
     * max,min,def
     * EXAMPLE2：
     * 19,1
     * max,min
     *
     * @Date 2016年7月1日
     * @since 2.5.0
     */
    public static LimitConfigVO parseLimitConfig(String configValue) {

        if (StringUtils.isBlank(configValue)) {
            return new LimitConfigVO();
        }
        String[] configs = configValue.split(",");
        Integer max = Integer.valueOf(configs[0].trim());
        Integer min = Integer.valueOf(configs[1].trim());
        Integer def = configs.length > 2 ? Integer.valueOf(configs[2].trim()) : null;
        return new LimitConfigVO(max, min, def);
    }

}
